package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements com.sky.service.ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前菜品或套餐是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果存在只需数量加一
        if(list != null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        }
        else{
            //如果不存在，则添加到购物车，数量默认就是一

            //判断当前添加的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //当前添加的是菜品
                Dish dish = dishMapper.getById(dishId); //查询当前id对应的套餐
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //当前添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();

                Setmeal setmeal = setmealMapper.getById(setmealId); //查询当前id对应的套餐
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1); //默认设置数量为1
            shoppingCart.setCreateTime(LocalDateTime.now()); // 设置当前创建时间
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public void cleanShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车单个商品
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前需要删除的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        Long userId = BaseContext.getCurrentId(); //获取当前操作的用户id
        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setDishId(dishId); //菜品id
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor()); //菜品口味
        shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId()); //套餐id
        shoppingCart.setUserId(userId); //用户id

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(dishId != null){
            //删除的是菜品

            //查看当前菜品数量
            Integer number = list.get(0).getNumber();
            //如果数量为1，则删除该菜品 大于1则数量减一
            if(number == 1){
                shoppingCartMapper.delete(shoppingCart);
            }else if(number > 1){
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.update(shoppingCart);
            }
        }else{
            //删除的是套餐

            //查看当前套餐数量
            Integer number = list.get(0).getNumber();

            if(number == 1){
                shoppingCartMapper.delete(shoppingCart);
            }else if(number > 1){
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
